package com.example.controller;

import com.example.common.Result;
import com.example.entity.Room;
import com.example.service.RoomService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @GetMapping("/selectAll")
    public Result selectAll(Room room){
        List<Room> roomslist=roomService.selectAll(room);
        return Result.success(roomslist);
    }

    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Room room){
        PageInfo<Room> roomPageInfo = roomService.selectPage(pageNum, pageSize,room);
        return Result.success(roomPageInfo);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Room room){
        roomService.add(room);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Room room){
        roomService.update(room);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        roomService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestParam String ids){
        // 将逗号分隔的字符串转换为 List<Integer>
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        roomService.deleteBatch(idList);
        return Result.success();
    }

}
